#include "VColorSelect.h"
#include "ui_VColorSelect.h"

#include <QColorDialog>

VColorSelect::VColorSelect(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::VColorSelect)
{
    ui->setupUi(this);
}

VColorSelect::~VColorSelect()
{
    delete ui;
}

void VColorSelect::set_color(QColor color)
{
    _color = makeColorString(color, "HEX");
    ui->btn_color->setStyleSheet(QString("QPushButton{background:#%1;margin:0px;padding:0px;border:1px solid black;}").arg(_color));
}

void VColorSelect::on_pushButton_clicked()
{

}

void VColorSelect::on_btn_color_clicked()
{
    QColor color = QColorDialog::getColor(QColor(_color.toInt(nullptr, 16)), this, "选择颜色");
    if(color.isValid())
    {
        _color = makeColorString(color, "HEX");
        ui->btn_color->setStyleSheet(QString("QPushButton{background:#%1;margin:0px;padding:0px;border:1px solid black;}").arg(_color));
        emit signal_color_changed();
    }

}

QString VColorSelect::makeColorString(const QColor &color, const QString type)
{
    if(type == "RGBA") {
        return QString("rgba(%1, %2, %3, %4)")
                .arg(color.red())
                .arg(color.green())
                .arg(color.blue())
                .arg(color.alpha());
    }
    if(type == "RGB") {
        return QString("rgba(%1, %2, %3)")
                .arg(color.red())
                .arg(color.green())
                .arg(color.blue());
    }
    if(type == "HEX") {
        return QString().sprintf("%02X%02X%02X",
                                 color.red(),
                                 color.green(),
                                 color.blue());
    }

    return color.name();
}
